/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.customize.security.utils.SecurityUtil;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.user.dto.AppFuelReceivePageDTO;
import com.pig4cloud.pigx.user.dto.FuelConsumePageDTO;
import com.pig4cloud.pigx.user.entity.FuelConsume;
import com.pig4cloud.pigx.user.enums.FuelConsumeEnum;
import com.pig4cloud.pigx.user.mapper.FuelConsumeMapper;
import com.pig4cloud.pigx.user.service.FuelConsumeService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import com.pig4cloud.pigx.user.vo.LoginUserFuelConsumeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 燃料消耗记录
 *
 * @author pigx code generator
 * @date 2022-11-24 17:37:06
 */
@Service
public class FuelConsumeServiceImpl extends ServiceImpl<FuelConsumeMapper, FuelConsume> implements FuelConsumeService {

	@Override
	public R<Page<LoginUserFuelConsumeVO>> getAppPage(AppFuelReceivePageDTO dto) {
		try {
			if (dto.getUserId() == null) {
				PigxUser user = SecurityUtil.getLoginUser();
				if (user == null) {
					throw new DuckweedException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
				}
				dto.setUserId(user.getId());
			}
			Page<FuelConsume> page = baseMapper.selectPage(new Page<FuelConsume>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}}, new QueryWrapper<FuelConsume>() {{
				eq("user_id", dto.getUserId());
				if (dto.getType() != null) {
					ge("type", dto.getType());
				}
				if (dto.getStrategyName() != null) {
					ge("strategy_name", dto.getStrategyName());
				}
				if (dto.getStartTime() != null) {
					ge("create_time", dto.getStartTime());
				}
				if (dto.getEndTime() != null) {
					le("create_time", dto.getEndTime());
				}
				orderByDesc("create_time");
			}});
			Page<LoginUserFuelConsumeVO> voPage = new Page<>();
			BeanUtils.copyProperties(page,voPage);
			List<LoginUserFuelConsumeVO> voList = new ArrayList<>();
			for (FuelConsume record : page.getRecords()) {
				LoginUserFuelConsumeVO vo = new LoginUserFuelConsumeVO();
				BeanUtils.copyProperties(record,vo);
				vo.setType(FuelConsumeEnum.Type.findName(vo.getType()));
				vo.setFuelQuantity(vo.getFuelQuantity().setScale(6, RoundingMode.DOWN));
				voList.add(vo);
			}
			voPage.setRecords(voList);
			return R.ok(voPage);
		} catch (DuckweedException e) {
			throw e;
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

	@Override
	public R<AdminPageDemoVO<FuelConsume>> pageList(FuelConsumePageDTO dto) {
		try {
			QueryWrapper<FuelConsume> wrapper = new QueryWrapper<FuelConsume>() {{
				if (dto.getUserId() != null) {
					like("user_id", dto.getUserId());
				}
				if (dto.getType() != null && !dto.getType().isEmpty()) {
					eq("type", dto.getType());
				}
				if (dto.getStrategyName() != null && !dto.getStrategyName().isEmpty()) {
					eq("strategy_name",dto.getStrategyName());
				}
				if (dto.getStartTime() != null) {
					ge("create_time", dto.getStartTime());
				}
				if (dto.getEndTime() != null) {
					le("create_time", dto.getEndTime());
				}
				orderByDesc("create_time");
			}};
			Page<FuelConsume> page = baseMapper.selectPage(new Page<FuelConsume>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}}, wrapper);
			for (FuelConsume record : page.getRecords()) {
				record.setType(FuelConsumeEnum.Type.findName(record.getType()));
				if (record.getNetProfit() != null) {
					record.setNetProfit(record.getNetProfit().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				}
				if (record.getGrossProfit() != null) {
					record.setGrossProfit(record.getGrossProfit().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				}
				if (record.getFuelQuantity() != null) {
					record.setFuelQuantity(record.getFuelQuantity().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				}
			}
			FuelConsume fuelConsume = baseMapper.selectOne(wrapper.select(
					"sum(fuel_quantity) 'fuelQuantity'," +
							"sum(gross_profit) 'grossProfit'," +
							"sum(net_profit) 'netProfit'"
			));
			if (fuelConsume != null) {
				if (fuelConsume.getNetProfit() != null) {
					fuelConsume.setNetProfit(fuelConsume.getNetProfit().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				}
				if (fuelConsume.getGrossProfit() != null) {
					fuelConsume.setGrossProfit(fuelConsume.getGrossProfit().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				}
				if (fuelConsume.getFuelQuantity() != null) {
					fuelConsume.setFuelQuantity(fuelConsume.getFuelQuantity().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				}
			}
			return R.ok(new AdminPageDemoVO<FuelConsume>(){{
				setPage(page);
				setStatistics(fuelConsume);
			}});
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

}
